package com.qf.java2102.qihaonan.hotel.controller.front;

import com.qf.java2102.qihaonan.hotel.entity.PageBean;
import com.qf.java2102.qihaonan.hotel.entity.ResultVO;
import com.qf.java2102.qihaonan.hotel.service.IFrontService;
import com.qf.java2102.qihaonan.hotel.service.impl.FrontServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author qhn
 * @version 1.0
 */
@WebServlet("/frontIndex")
public class FrontIndexController extends HttpServlet {

    private IFrontService frontService = new FrontServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String currentPageStr = request.getParameter("currentPage");
        String foodName = request.getParameter("foodName");
        String typeIdStr = request.getParameter("typeId");

        Integer currentPage =  Integer.valueOf(currentPageStr == null ? "1" : currentPageStr);
        Long typeId = Long.valueOf(typeIdStr == null ? "0" : typeIdStr);
        ResultVO<PageBean> resultVO = frontService.pageBean(currentPage, foodName, typeId);
        if(resultVO.getSuccess()) {
            request.setAttribute("pageBean", resultVO.getData());
            //搜索条件回传
            request.setAttribute("foodName", foodName);
            request.setAttribute("typeId", typeId);
            request.getRequestDispatcher("/front/detail/caidan.jsp").forward(request, response);
            return;
        }
        response.getWriter().write(resultVO.getMessage());

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
